<?php
/*
    Dill is a tool for creating websites with PHP.
    Copyright (C) 2014  Jannik Haberbosch

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


function array_remove( $arr, $value )
{
	/* Attempts to removes an element with a given value '$value' from the array
	'$arr', and returns the array.
	On failure, 'FALSE' is returned.

	Parameters		$arr
					array
					The array from which to remove the value "$value" from.

					$value
					string
					The value to remove from "$arr".

	Return values	Array or FALSE
					On success, the modified array is returned; on failure FALSE.
					
	*/
	$temparr = array();
	foreach( $arr as $item )
	{
		if( strcmp( $item, $value ) == 0 )
		{
			continue;
		}
		else
		{
			$temparr[] = $item;
		}
	}
	return $temparr;	
}


function array_remove_duplicates( $array )
{
	/* Tries to remove duplicate entries from an array and returns a newly
	constructed array without the duplicate entries.
	On failure, FALSE is returned.

	Parameters		$array
					array
					An array that contains several elements.

	Return values	Array
					The new constructed array without any duplicates.

					FALSE
					boolean
					Returned if an error has occured.

	*/
	// '$array' needs to be an Array of course.
	if( !is_array( $array ) ) return FALSE;
	
	$new_array = array();
	foreach( $array as $element )
	{
		// Case-sensitive checking!
		if( !in_array( $new_array, $element ) )
		{
			$new_array[] = $element;
		}
	}

	return $new_array;
}


function array_columns( $array, $cols = NULL )
{
	/* Returns a new array with the columns specified.  Columns can only
	be based on an index.
	If no columns are specified, 'FALSE' is returned.
	If an error occurs, 'FALSE' is returned.

	Parameters		$array
					array
					The array from which to extract certain columns.

					$cols
					array
					An array of integers which define which columns to extract
					and put into the new array.

	Return values	array or FALSE
					In case of an error, FALSE is returned.
	
	*/
	// Each array inside "$array" has to be of the same size
	if( !is_array( $array ) ) return FALSE;
	if( count( $array ) == 0 ) return FALSE;
	if( !is_array( $array[0] ) ) return FALSE;
	if( count( $array[0] ) == 0 ) return FALSE;

	// Only index based columns may be specified.  Thus I check for an integer.
	if( array_reduce( "is_int", $cols, TRUE ) == FALSE ) return FALSE;

	// The user has to specify at least one column.
	if( count( $cols ) == 0 ) return FALSE;

	// Specifying more columns to fetch than are available is invalid, and
	// thus, 'FALSE' is returned:
	$arr_length = count( $array[0] );
	$cols = array_remove_duplicates( $cols );
	
	if( max( $cols ) > $arr_length - 1 ) return FALSE;

	foreach( $array as $element )
	{
		if( !is_array( $element ) ) return FALSE;
		$new_arr_length = count( $element );

		// All arrays inside the parent array need to be of the same length.
		if( $new_arr_length != $arr_length ) return FALSE;
		$arr_length = $new_arr_length;
	}

	// The new array is constructed here.
	$new_array = array();
	$counter = 0;
	foreach( $array as $element )
	{
		$new_array[$counter] = array();
		foreach( $cols as $c )
		{
			$new_array[$couter][] = $element[$c];
		}
		$counter++;
	}

	return $new_array;
}


function get_by_property_value( $arr, $property, $value )
{
	/* This function returns an object from the array '$arr', if it is has a
	property '$property', to which a value '$value' has been assigned.
	Otherwise, a boolean FALSE is returned.

	In case of an error, NULL is returned.

	Parameters		$arr
					array
					An array that contains several objects.

					$property
					string
					An object should have a property with that name.

					$value
					mixed
					An object's property should have a value '$value'.

	Return values	Object, FALSE or NULL
					The object is returned on success, FALSE if no matching
					object could be found.
					In case of an error, NULL is returned.

	*/

	// Make sure that every element is an object.
	if( !array_reduce( $arr, function( $carry, $item ){ return $carry && is_object( $item ); }, TRUE ) )
	{
		return NULL;
	}
	foreach( $arr as $element )
	{
		if( property_exists( $element, $property ) )
		{
			if( strcmp( $value, $element->get_property( $property ) ) == 0 )
			{
				return $element;
			}
		}
	}

	// No appropriate element could be found.
	return FALSE;
}

?>
